//
//  LHProductsTableViewController.m
//  EPA
//
//  Created by 卢浩 on 16/4/19.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHProductsTableViewController.h"
#import "LHNetWorkRequestManager.h"
#import "LHBaseModel.h"
#import "LHProductsModel.h"
#import "LHBaseTableViewCell.h"
#import "LHBaseWebViewController.h"
#import "LORefresh.h"

@interface LHProductsTableViewController ()

@property (nonatomic,strong) NSMutableArray *productsData;

@end

@implementation LHProductsTableViewController

- (void)dealloc
{
    [self.tableView removeObserver:[self.tableView defaultHeader] forKeyPath:@"contentOffset"];
    
    //底部
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"良  品";
    
    self.productsData = [NSMutableArray array];
    
    [self loadDataWithStart:0];
    
    //添加下拉刷新
    __weak typeof(self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        [weakSelf.productsData removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf loadDataWithStart:0];
    }];
    
    //添加上提加载更多
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.productsData.count;
        [weakSelf loadDataWithStart:count];
    }];
}

- (void)loadDataWithStart:(NSInteger)start{
    NSDictionary *paramsDict = @{@"start":@(start),@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"limit":@(10),@"auth":@"",@"version":@"3.0.2"};
    [LHNetWorkRequestManager requestUrl:[NSURL URLWithString:SHOPLIST_URL] Methed:POST Params:paramsDict Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *topicsDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dataDict = topicsDict[@"data"];
            NSArray *listArray = dataDict[@"list"];
            NSMutableArray *temp = [NSMutableArray array];
            for (NSDictionary *topsicDict in listArray) {
                LHBaseModel *model = [LHBaseModel modelWithDict:topsicDict];
                [temp addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.productsData addObjectsFromArray:temp];
                [self.tableView reloadData];
                //刷新完成
                [[self.tableView defaultHeader] endRefreshing];
                //底部
                [[self.tableView defaultFooter] endRefreshing];
            });
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.productsData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LHBaseTableViewCell *cell = [LHBaseTableViewCell cellWithTableView:tableView Model:self.productsData[indexPath.row] IndexPath:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 330;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LHBaseWebViewController *webVC = [[LHBaseWebViewController alloc]init];
    LHProductsModel *model = self.productsData[indexPath.row];
    webVC.url = model.buyurl;
    webVC.navigationItem.title = @"片 刻 生 活 馆";
    [self.navigationController pushViewController:webVC animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
